from random import *
from copy import deepcopy
from Posicion import Posicion

class Grilla:
	def __init__(self, matrizConLetras):
		
		self.__alto = len(matrizConLetras)
		self.__ancho = len(matrizConLetras[0])
		self.__grilla = deepcopy(matrizConLetras)
		
	def ancho(self):
		return self.__ancho

	def alto(self):
		return self.__alto


	def letraEn(self, posicion):
		return self.__grilla[posicion.x()][posicion.y()]

	def palabraEnMarca(self, marca):
		posicionInicial = marca.posicionInicial()
		posicionFinal = marca.posicionFinal()
		difX = float(posicionFinal.x()) - float(posicionInicial.x())
		difY = float(posicionFinal.y()) - float(posicionInicial.y())

		z = max (abs(difX), abs(difY))

		vel = Posicion(difX/z, difY/z)

		palabraEncontrada = ""
		for i in range(int(z+1)):
			palabraEncontrada += self.__grilla[int(posicionInicial.x()+ i*vel.x())][int(posicionInicial.y() + i*vel.y())]

		return palabraEncontrada
	
	def __repr__(self):
		repr = ""
		for fila in self.__grilla:
			for letra in fila:
				repr += letra
			repr += '\n'
		return repr


